<?php
include_once('config/config.php');

$where = 1;
//接收地址的搜索关键字参数
$keyword = $_GET['keyword'];
if(!empty($keyword)){
    $where = "j.`name` LIKE '%$keyword%'";
}

//获取地址的当前页参数
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取当前表里数据总数
$count = dbFind("SELECT COUNT(*) AS num FROM `job` as j WHERE $where");

// pre($count);
// exit;
// 每页显示条数
$limit = 3;

// 页码
$size = 3;

$showPage = page($page,$count['num'],$limit,$size,'yahoo2');

$start = ($page - 1)*$limit;

if(!empty($keyword)){
    $where = "(j.`name` LIKE '%$keyword%') or (d.`name` LIKE '%$keyword%')";
}

//查询的SQL语句
$sql = "SELECT j.`id`,j.`name`,d.`name` as dname  FROM `job` as j LEFT JOIN `department` as d ON j.`depid` = d.`id` WHERE $where ORDER BY j.`id` ASC LIMIT $start,$limit";
$allData = dbAll($sql);

if($_POST){
    $action = $_POST['action'];
    if($action == 'del'){
        $id = $_POST['id'];
        $admin = dbFind("SELECT * FROM `job` WHERE `id` = '$id'");
        if(!$admin){
            error("该职位无法删除");
        }
        //登录的管理员是否是被删除，没有登录功能
        $result = dbDel('job',"`id` = '$id'");
        if($result){
            success('删除成功');
        } else {
            error('删除失败');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>职位</title>
    <!-- 引用公共头部 -->
    <?php include_once('common/meta.php'); ?>

    <style>
        .icon-plus {
            margin-right: 5px;
        }
        .icon-pencil{
            position: absolute;
            margin-left: -30px;
        }
        .well{
            padding-bottom: 0;
        }
        .table{
            margin-bottom: 0;
        }
        .search {
            margin-left: 10px;
        }

        .btn-info:focus {
            outline: none;
        }
    </style>
</head>

<body>
    <title>职位</title>

    <!-- 引用公共头部 -->
    <?php include_once('common/header.php'); ?>

    <!-- 引用侧边菜单 -->
    <?php include_once('common/sidebar.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">职位列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">首页</a> <span class="divider">/</span></li>
            <li class="active">职位</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar" style="display:flex;height:40px">
                    <button class="btn btn-primary" onClick="location='jobAdd.php'" style="height: 30px;"><i class="icon-plus"></i>添加职位</button>
                    <form method="get">
                        <input class="search" type="search" name="keyword" placeholder="请输入搜索职位或部门">
                        <button class="btn btn-info" style="margin-top: -8px;background-position: 0;">搜索</button>
                    </form>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>职位</th>
                                <th>部门</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                                foreach($allData as $data){
                                    $data['dname'] = $data['dname']?$data['dname']:"无组织";
                                    $strTD = <<<TD
                                    <tr>
                                        <td style='vertical-align: middle;'>{$data['id']}</td>
                                        <td style='vertical-align: middle;'>{$data['name']}</td>
                                        <td style='vertical-align: middle;'>{$data['dname']}</td>
                                        <td>
                                            <a href="jobEdit.php?id={$data['id']}&dname={$data['dname']}"><i class="icon-pencil"></i></a>
                                            
                                            <a href="#myModal" class="del" data-id="{$data['id']}" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                        </td>
                                    </tr>
TD;
                                    echo $strTD;
                                }
                            ?>
                        </tbody>
                    </table>
                </div>
                <!-- <div class="pagination">
                    <ul>
                        <li><a href="#">上一页</a></li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">下一页</a></li>
                    </ul>
                </div> -->

                <?php echo $showPage;?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除确认</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>您确定要删除该职位吗？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-id="" data-dismiss="modal">删除</button>
                    </div>
                </div>

                <!-- 引用公共底部 -->
                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>
</body>

</html>
<!-- 引用script -->
<?php include_once('common/script.php'); ?>
<script>
    $('.del').click(function(){
        let id = $(this).data('id')
        $('.btn-danger').attr('data-id',id)
    });

    $('.btn-danger').click(function(){
        let id = $(this).data('id')

        //发起异步请求
        $.ajax({
            // 请求方法 get post 一般都是用post
            type:'post',
            // 请求地址
            url:'jobList.php',
            // 请求需要的参数
            data:{
                id,//id:id
                action:'del'
            },
            // 请求返回的数据类型
            dataType:'json',// html text xml json
            // 请求成功的回调函数 http 状态码 = 200 
            success:function(res){
                if(res.code === 1){
                    alert(res.msg)
                    location.reload()
                } else {
                    alert(res.msg)
                }
            },
            // 请求失败的回调函数 404 301 500等
            error:function(error){
                console.log(error)
            }
        })
    })
</script>